﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'P_Rpt_UserSession_History')
	BEGIN
		DROP  Procedure  [esf_sso].P_Rpt_UserSession_History
	END

GO

-- [esf_sso].P_Rpt_UserSession_History 'root'
CREATE Procedure [esf_sso].P_Rpt_UserSession_History
	@pUserAlias VARCHAR (MAX) = NULL
AS

	SELECT
		VU.Usr_Alias
		,VU.Usr_Per_cal_FullName
		,VU.Usr_Per_Ent_Name
		,VU.Usr_Per_Email
		,VU.Usr_Per_IT_Code
		,VU.Usr_Per_IdentificationNumber
		,USL.StartDateTime AS UsrSession_LastStartDateTime
		,USL.UserHostAddress AS UsrSession_LastUserHostAddress
	FROM 
		esf_sso.VUser VU
	INNER JOIN
		esf_sso.UserSessionLog USL ON
			USL.UserPersonID = VU.Usr_Per_ID
	WHERE
		(@pUserAlias IS NULL OR VU.Usr_Alias = @pUserAlias)
	ORDER BY
		USL.StartDateTime DESC
